///*
// * To change this license header, choose License Headers in Project Properties.
// * To change this template file, choose Tools | Templates
// * and open the template in the editor.
// */
//package com.iti.jets.gpts.dto.assist;
//
//import com.iti.jets.gpts.home.PointHome;
//import com.iti.jets.gpts.home.TranServiceHome;
//import com.iti.jets.gpts.pojo.Point;
//import com.iti.jets.gpts.pojo.TranService;
//import java.util.ArrayList;
//import java.util.List;
//import org.hibernate.Hibernate;
//import org.hibernate.Session;
//
///**
// *
// * @author mustafajnr
// */
//public class Business {
//    
//    TranServiceHome tranServiceDAO;
//    PointHome pointDAO;
//
//    public Business(TranServiceHome tranServiceHome, PointHome pointHome) {
//        this.tranServiceDAO = tranServiceHome;
//        this.pointDAO = pointHome;
//    }
//
//    public Point getPoint(Integer group) {
//        return pointDAO.read(group);
//    }
//
//    public TranService getTranService(Integer group) {
//        return tranServiceDAO.read(group);
//    }
//}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.dto.assist;

import util.HibernateUtil;
import com.iti.jets.gpts.pojo.Point;
import com.iti.jets.gpts.home.PointHome;
import com.iti.jets.gpts.pojo.TranService;
import com.iti.jets.gpts.home.TranServiceHome;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.type.ArrayType;

/**
 *
 * @author mustafajnr
 */
public class Business {

    static TranServiceHome tranServiceDAO;
    static PointHome pointDAO;
    public static Business business;

    static {
        tranServiceDAO = new TranServiceHome(HibernateUtil.getSessionFactory());
        pointDAO = new PointHome(HibernateUtil.getSessionFactory());
    }

    public static List<TranServiceFullUsage> search(Integer point1, Integer point2, Integer numberOfServices) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TranServiceFullUsage> results = new ArrayList<TranServiceFullUsage>();
        for (Object result : session.createSQLQuery("SELECT * FROM main.find_routes_bfs(:point1,:point2,:nos)")
                .addScalar("route", Hibernate.TEXT)
                .setParameter("point1", point1)
                .setParameter("point2", point2)
                .setParameter("nos", numberOfServices + 1)
                .list()) {
            results.add(new TranServiceFullUsage((String) result));
        }
        return results;
    }

    public static List<TranServiceFullUsage> search(String point1, String point2, Integer numberOfServices) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TranServiceFullUsage> results = new ArrayList<TranServiceFullUsage>();
        Integer p1 = ((Point) pointDAO.findByName(point1).get(0)).getId();
        Integer p2 = ((Point) pointDAO.findByName(point2).get(0)).getId();
        for (Object result : session.createSQLQuery("SELECT * FROM main.find_routes_bfs(:point1,:point2,:nos)")
                .addScalar("route", Hibernate.TEXT)
                .setParameter("point1", p1)
                .setParameter("point2", p2)
                .setParameter("nos", numberOfServices + 1)
                .list()) {
            results.add(new TranServiceFullUsage((String) result));
        }
        return results;
    }

    public static Point getPoint(Integer group) {
        return pointDAO.read(group);
    }

    public static TranService getTranService(Integer group) {
        return tranServiceDAO.read(group);
    }

    public static void main(String[] args) {
        SessionFactory sessionFactory = new Configuration().configure("com/iti/jets/gpts/config/hibernate.cfg.xml").buildSessionFactory();
        TranServiceHome t = new TranServiceHome(sessionFactory);
        PointHome p = new PointHome(sessionFactory);

        List<TranServiceFullUsage> ff = Business.search("ram", "elwa", 2);
        for (int i = 0; i < ff.size(); i++) {
            System.out.println(ff.get(i));
        }
    }

    public static String routeSearch(Point point1, Point point2, Integer numberOfServices) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        return (String) session.createSQLQuery("SELECT to_json(array(select row_to_json(breadth_first_search(:point1,:point2,0,:nos)))) result")
                .addScalar("result", Hibernate.TEXT)
                .setParameter("point1", point1)
                .setParameter("point2", point2)
                .setParameter("nos", numberOfServices + 1)
                .uniqueResult();
    }

    public static Point getPoint(String point) {
        return pointDAO.findByExactName(point);
    }

    public static String getRouteDetails(String route) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        return (String) session.createSQLQuery("SELECT to_json(array_agg(to_json(result))) res from get_route_details(:route)")
                .addScalar("res", Hibernate.TEXT)
                .setParameter("route", route)
                .uniqueResult();
    }
}
